package com.metersbonwe.basetest.core;

import java.util.concurrent.CountDownLatch;

import org.junit.Test;

public abstract class Task extends JUnitTestRunner implements Runnable {

	public int index = 0;
	
	/**
	 * 运行次数
	 */
	private int runCount = 10000;

	/**
	 * 总时间
	 */
	private long alltime = 0l;

	public static long lvl1 = 0l;
	public static long lvl2 = 10l;
	public static long lvl3 = 50l;
	public static long lvl4 = 100l;
	public static long lvl5 = 200l;

	/*
	 * 各时间段次数
	 */
	private long lvl1_c = 0l;
	private long lvl2_c = 0l;
	private long lvl3_c = 0l;
	private long lvl4_c = 0l;
	private long lvl5_c = 0l;
	private long lvl6_c = 0l;
	private long s_count = 0; // 成功次数

	private CountDownLatch countDownLatch;

	public Task(int runCount, CountDownLatch countDownLatch) {
		super();
		this.runCount = runCount;
		this.countDownLatch = countDownLatch;
	}

	public Task() {
	}

	public void run() {
		
		init();

		for (int i = 0; i < runCount; i++) {
			long b = System.currentTimeMillis();
			try {
				if (task())
					s_count++;
			} catch (Exception e) {
				e.printStackTrace();
			}
			long e = System.currentTimeMillis();
			saveTime(e - b);// 保存时间分布
		}

		destroy();

		countDownLatch.countDown();

		System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + " : thread Down");
	}

	private double lvlall_f = 0;

	private void saveTime(long time) {
		alltime += time;
		if (time <= lvl1) {
			lvl1_c++;
			return;
		}
		if (time <= lvl2) {
			lvl2_c++;
			return;
		}
		if (time <= lvl3) {
			lvl3_c++;
			return;
		}
		if (time <= lvl4) {
			lvl4_c++;
			return;
		}
		if (time <= lvl5) {
			lvl5_c++;
			return;
		}
		lvl6_c++;
	}

	/**
	 * 任务
	 * 
	 * @return
	 */
	public abstract boolean task();

	/**
	 * 初始化
	 */
	public abstract void init();

	/**
	 * 回收
	 */
	public abstract void destroy();

	public int getRunCount() {
		return runCount;
	}

	public void setRunCount(int runCount) {
		this.runCount = runCount;
	}

	public long getAlltime() {
		return alltime;
	}

	public void setAlltime(long alltime) {
		this.alltime = alltime;
	}

	public long getLvl1_c() {
		return lvl1_c;
	}

	public void setLvl1_c(long lvl1_c) {
		this.lvl1_c = lvl1_c;
	}

	public long getLvl2_c() {
		return lvl2_c;
	}

	public void setLvl2_c(long lvl2_c) {
		this.lvl2_c = lvl2_c;
	}

	public long getLvl3_c() {
		return lvl3_c;
	}

	public void setLvl3_c(long lvl3_c) {
		this.lvl3_c = lvl3_c;
	}

	public long getLvl4_c() {
		return lvl4_c;
	}

	public void setLvl4_c(long lvl4_c) {
		this.lvl4_c = lvl4_c;
	}

	public long getLvl5_c() {
		return lvl5_c;
	}

	public void setLvl5_c(long lvl5_c) {
		this.lvl5_c = lvl5_c;
	}

	public long getLvl6_c() {
		return lvl6_c;
	} 

	public void setLvl6_c(long lvl6_c) {
		this.lvl6_c = lvl6_c;
	}

	public long getS_count() {
		return s_count;
	}

	public void setS_count(long s_count) {
		this.s_count = s_count;
	}

	public CountDownLatch getCountDownLatch() {
		return countDownLatch;
	}

	public void setCountDownLatch(CountDownLatch countDownLatch) {
		this.countDownLatch = countDownLatch;
	}

	public double getLvlall_f() {
		return lvlall_f;
	}

	public void setLvlall_f(double lvlall_f) {
		this.lvlall_f = lvlall_f;
	}
	
	@Test
	public void runner() {
		System.out.println(1);
	}

}
